﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace WPNextTools.NControls
{
    public class SeqPlayImg : Control
    {
        #region Fields
        private Grid layoutRoot;
        private Canvas frame;
        private Grid film;
        private Image imageNext;
        private string _imageFilesExtension = "png";
        #endregion

        #region Constructor
        public SeqPlayImg()
        {
            base.DefaultStyleKey = typeof(SeqPlayImg);
        }

        #endregion

        #region ApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.layoutRoot = (Grid)base.GetTemplateChild("LayoutRoot");
            this.frame = (Canvas)base.GetTemplateChild("Frame");
            this.film = (Grid)base.GetTemplateChild("Film");
            this.imageNext = (Image)base.GetTemplateChild("ImageNext");
        }
        #endregion

        #region DependecyProperties

        #region ImageFolder
        public string ImageFolder
        {
            get { return (string)GetValue(ImageFolderProperty); }
            set { SetValue(ImageFolderProperty, value); }
        }

        public static readonly DependencyProperty ImageFolderProperty =
            DependencyProperty.Register("ImageFolder", typeof(string), typeof(SeqPlayImg), new PropertyMetadata(null));
        #endregion

        #region CurrentImgNum
        public double CurrentImgNum
        {
            get { return (double)GetValue(CurrentImgNumProperty); }
            set { SetValue(CurrentImgNumProperty, value); }
        }

        public static readonly DependencyProperty CurrentImgNumProperty =
            DependencyProperty.Register("CurrentImgNum", typeof(double), typeof(SeqPlayImg), new PropertyMetadata(1.0, OnCurrentImgNumChanged));

        private static void OnCurrentImgNumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as SeqPlayImg).OnCurrentImgNumChanged(e);
        }

        private void OnCurrentImgNumChanged(DependencyPropertyChangedEventArgs e)
        {
            this.UpdateSequence();
        }
        #endregion
        
        #endregion

        #region Properties
        public string ImageFilesExtension
        {
            get { return _imageFilesExtension; }
            set { _imageFilesExtension = value; }
        }
        #endregion

        #region Method
        
        private void UpdateSequence()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                base.Dispatcher.BeginInvoke(() =>
                    {
                        this.imageNext.Source = new BitmapImage(new Uri(string.Concat(this.ImageFolder, Math.Floor(this.CurrentImgNum), ".", this.ImageFilesExtension), UriKind.RelativeOrAbsolute));
                    });
            }
        }
        #endregion

    }
}
